package com.jzy.service.impl;

import com.jzy.dao.MemberDao;
import com.jzy.pojo.Member;
import com.jzy.service.MemberService;
import com.jzy.utils.MD5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service // Spring的
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = MemberService.class) // Dubbo的
@Transactional
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberDao memberDao;

    //根据手机号查询会员
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for (String m : month) {
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    //新增会员
    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            //用MD5Utils将明文密码进行加密
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }
}
